home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Acere (Card Game) / AcereÄ.sit / Acereƒ / CTextDoc.cp < prev    next >
Text File  |  1994-08-25  |  8KB  |  318 lines

  1. // ===========================================================================
  2. //    CTextDoc.cp                        ⌐1994 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    A simple text document class. It can handle opening, saving, and
  6. //    reverting TEXT files. It displays the text in a Window and can
  7. //    print the text.
  8.  
  9. #include "AcereApp.h"
  10. #include "CTextDoc.h"
  11. #include "WellStack.h"
  12. #include "WellFreeCell.h"
  13. #include "WellDeck.h"
  14. #include "CardDeck.h"
  15.  
  16. #include <LWindow.h>
  17. #include <LFile.h>
  18. #include <LPrintout.h>
  19. #include <LPlaceHolder.h>
  20. #include <UMemoryMgr.h>
  21. #include <UWindows.h>
  22. #include <String_Utils.h>
  23. #include <LTextEdit.h>
  24.  
  25. #include <Sound.h>
  26.  
  27. CTextDoc    *theDoc;
  28.  
  29. const ResIDT    WIND_TextDoc        = 500;
  30. const ResIDT    prto_TextDoc        = 201;
  31.  
  32. #define kVictorySound 1000
  33. //    const OSType    Creator_DemoDoc        = 'PPd0';
  34. const ResIDT    STRx_Untitled        = 300;
  35.  
  36.  
  37. // ---------------------------------------------------------------------------
  38. //        Ñ CTextDoc(LCommander*, FSSpec*)
  39. // ---------------------------------------------------------------------------
  40. //    Construct a TextDoc associated with the specified file
  41. //
  42. //    If inFileSpec is nil, then create an empty, untitled document
  43.  
  44.  
  45. CTextDoc::CTextDoc(
  46.     LCommander    *inSuper,
  47.     FSSpec        *inFileSpec)
  48.         : LSingleDoc(inSuper)
  49. {
  50. //    CGrafPtr         theMacPort;
  51. //    PixPatHandle    thePixPat;
  52.     
  53. //    short    i;
  54.     
  55.     theDeck = new CardDeck;
  56.     
  57.     currentDeckWell = currentFreeCell = currentStack = 0;
  58.     
  59.     cardRgn = NewRgn();
  60.     theDoc = this;
  61.     
  62.     firstCard.card = secondCard.card = kNoCard;
  63.  
  64.  
  65.                                     // Create window for our document
  66.     mWindow = LWindow::CreateWindow(WIND_TextDoc, this);
  67.     
  68.                                     // Specify that the text view should
  69.                                     // be the Target when the Window
  70.                                     // is activated
  71. //    mTextView = (CDirtyText*) mWindow->FindPaneByID('Text');
  72. //    mWindow->SetLatentSub(mTextView);
  73.     
  74.     if (inFileSpec == nil) {
  75.         NameNewDoc();                // Set name of untitled window
  76.         
  77.     } else {
  78.         OpenFile(*inFileSpec);        // Display contents of file in window
  79.     }
  80.     
  81. //    theMacPort = (CGrafPtr)mWindow->GetMacPort();
  82. //    SetPort((GrafPtr)theMacPort);
  83.     
  84. //    thePixPat = GetPixPat(6009);
  85. //    HLock((Handle)thePixPat);
  86.     
  87. //    ((CGrafPtr)mWindow)->bkPixPat = GetPixPat(6009);
  88. //    SetPortPix((*thePixPat)->patMap);
  89. }
  90.  
  91. CTextDoc::~CTextDoc()            //    destructor
  92. {
  93.     delete theDeck;
  94.     theDeck = nil;
  95.     
  96.     theDoc = nil;
  97. }
  98.  
  99. // ---------------------------------------------------------------------------
  100. //        Ñ NameNewDoc
  101. // ---------------------------------------------------------------------------
  102. //    Name a new, untitled document window
  103. //
  104. //    Untitled windows start with "untitled", then "untitled 1",
  105. //    "untitled 2", etc. Old numbers are reused, so there won't be
  106. //    gaps in the numbering.
  107. //
  108. //    This routine uses a STR# resource to store the "untitled" string,
  109. //    which can be localized to different languages. The first string
  110. //    is "untitled" and the second is "untitled " (trailing space),
  111. //    which is used when appending a number to the name.
  112.  
  113. void
  114. CTextDoc::NameNewDoc()
  115. {
  116.         // Start with the default name ("untitled")
  117.     Str255    name;
  118.     ::GetIndString(name, STRx_Untitled, 1);
  119.     
  120.     long    num = 0;
  121.     while (UWindows::FindNamedWindow(name) != nil)
  122.     {
  123.             
  124.             // An existing window has the current name
  125.             // Increment counter and try again
  126.  
  127.         ::GetIndString(name, STRx_Untitled, 2);
  128.         num++;
  129.         Str15    numStr;
  130.         ::NumToString(num, numStr);
  131.         ConcatPStr(name, numStr);
  132.     }        
  133.     
  134.     mWindow->SetDescriptor(name);        // Finally, set window title
  135.     
  136. //    PlaceCards();
  137. }
  138.  
  139.  
  140. // ---------------------------------------------------------------------------
  141. //        Ñ OpenFile
  142. // ---------------------------------------------------------------------------
  143. //    Open a new document for the specified File
  144.  
  145. void
  146. CTextDoc::OpenFile(
  147.     FSSpec    &inFileSpec)
  148. {
  149.         // Create a new File object, read the entire File contents,
  150.         // put the contents into the text view, and set the Window
  151.         // title to the name of the File.
  152.         
  153.     Try_ {
  154.         mFile = new LFile(inFileSpec);
  155.         mFile->OpenDataFork(fsRdWrPerm);
  156.         Handle    textH = mFile->ReadDataFork();
  157.         mTextView->SetTextHandle(textH);
  158.         ::DisposeHandle(textH);
  159.         
  160.         mWindow->SetDescriptor(inFileSpec.name);
  161.         mIsSpecified = true;
  162.     }
  163.     
  164.     Catch_(inErr) {
  165.         delete this;
  166.         Throw_(inErr);
  167.     
  168.     } EndCatch_
  169. }
  170.  
  171.  
  172. // ---------------------------------------------------------------------------
  173. //        Ñ IsModified
  174. // ---------------------------------------------------------------------------
  175. //    Return whether the Document is has changed since the last save
  176.  
  177. Boolean
  178. CTextDoc::IsModified()
  179. {
  180.         // Document has changed if the text view is dirty
  181. //    mIsModified = mTextView->IsDirty();
  182.     return mIsModified;
  183. }
  184.  
  185.  
  186. // ---------------------------------------------------------------------------
  187. //        Ñ DoAESave
  188. // ---------------------------------------------------------------------------
  189. //    Save Document in the specified file with the specified file type
  190. //
  191. //    If file type is fileType_Default, use the normal file type for
  192. //    this document
  193.  
  194. void
  195. CTextDoc::DoAESave(
  196.     FSSpec    &inFileSpec,
  197.     OSType    inFileType)
  198. {
  199.     delete mFile;                        // Kill existing file
  200.     
  201.     mFile = new LFile(inFileSpec);        // Make new file object
  202.     
  203.     OSType    fileType = GameFileType;            // Find proper file type
  204.     if (inFileType != fileType_Default)
  205.     {
  206.         fileType = inFileType;
  207.     }
  208.                                         // Make new file on disk
  209.     mFile->CreateNewDataFile(OurCreator, GameFileType, 0);
  210.     mFile->OpenDataFork(fsRdWrPerm);
  211.     DoSave();                            // Write out data
  212.                                         // Change window name
  213.     mWindow->SetDescriptor(inFileSpec.name);
  214. }
  215.  
  216.  
  217. // ---------------------------------------------------------------------------
  218. //        Ñ DoSave
  219. // ---------------------------------------------------------------------------
  220. //    Save the entire Document to its associated File (which must already exist)
  221.  
  222. void
  223. CTextDoc::DoSave()
  224. {
  225.                                         // Get text and write to file
  226.     Handle    textH = mTextView->GetTextHandle();
  227.     StHandleLocker    theLock(textH);
  228.     mFile->WriteDataFork(*textH, GetHandleSize(textH));
  229.     
  230. //    mTextView->SetDirty(false);            // Saving makes doc un-dirty
  231. }
  232.  
  233.  
  234. // ---------------------------------------------------------------------------
  235. //        Ñ DoRevert
  236. // ---------------------------------------------------------------------------
  237. //    Revert the Document to the last saved version on disk
  238.  
  239. void
  240. CTextDoc::DoRevert()
  241. {
  242.     Handle    textH = mFile->ReadDataFork();
  243.     mTextView->SetTextHandle(textH);
  244.     ::DisposeHandle(textH);
  245.     mTextView->Refresh();
  246. }
  247.  
  248.  
  249. // ---------------------------------------------------------------------------
  250. //        Ñ DoPrint
  251. // ---------------------------------------------------------------------------
  252. //    Print the contents of the Document
  253.  
  254. void
  255. CTextDoc::DoPrint()
  256. {
  257.     LPrintout        *thePrintout = LPrintout::CreatePrintout(prto_TextDoc);
  258.     LPlaceHolder    *textPlace = (LPlaceHolder*)
  259.                                     thePrintout->FindPaneByID('TBox');
  260.     textPlace->InstallOccupant(mTextView, atNone);
  261.     
  262.     thePrintout->DoPrintJob();
  263.     delete thePrintout;
  264. }
  265.  
  266. void    CTextDoc::CheckVictory(void)
  267. {
  268.     short            i;
  269.     Handle             mySndHandle;            // handle to an 'snd ' resource
  270.     SndChannelPtr    mySndChan;        // pointer to a sound channel
  271.     OSErr            myErr;
  272.  
  273.     
  274.     
  275.     for (i= 0; i < 4; i++)
  276.     {
  277.         if (theDeckWells[i]->itsCard.card != 13)        // it's not a king
  278.             return;
  279.     }
  280.     
  281.     
  282.     mySndChan = nil;     // Initialize channel pointer for error checking
  283.  
  284.     // Read in 'snd' resource from resource file
  285.     mySndHandle = GetResource ('snd ', kVictorySound);
  286.  
  287.     // check for a nil handle
  288.     if ( mySndHandle != nil )
  289.     {
  290.         myErr = SndPlay (mySndChan, mySndHandle, true);
  291.     }
  292. }
  293.  
  294. void    CTextDoc::StartNewGame(void)
  295. {
  296.     short    i;
  297.     
  298.         theDeck->ZapOldDeck();
  299.         theDeck->GenerateNewDeck();
  300.         theDoc->currentDeckWell = theDoc->currentFreeCell = theDoc->currentStack = 0;
  301.  
  302.     for (i= 0; i < 4; i++)
  303.     {
  304.         theDeckWells[i]->itsCard.card = kNoCard;
  305.         theDeckWells[i]->Draw(nil);
  306.         
  307.         theFreeCells[i]->itsCard.card = kNoCard;
  308.         theFreeCells[i]->Draw(nil);
  309.     }
  310.     
  311.     for (i=0; i < 8; i++)
  312.     {
  313.         theStacks[i]->InitCards();
  314.     }
  315.     
  316. }
  317.  
  318.